cat('Creating Tables A11 and A12 \n\n')



white.dat = fread('TablesA11A12white_dat.csv')
non.south.white.dat = white.dat[white.dat$south == 0,]


reg.white = lm(voted2012~diss*nonwhite.to.white+diss*I(nonwhite.to.white^2)+gender+familyincome+partyaffiliation+state,
          data = white.dat,
          weights = count)
clustered.se = cl(white.dat, reg.white, white.dat$dma_ID)
reg.white$se = clustered.se

reg.white.non.south = lm(voted2012~diss*nonwhite.to.white+diss*I(nonwhite.to.white^2)+gender+familyincome+partyaffiliation+state,
               data = non.south.white.dat,
               weights = count)
clustered.se = cl(non.south.white.dat, reg.white.non.south, non.south.white.dat$dma_ID)
reg.white.non.south$se = clustered.se

rm(list = c("white.dat","non.south.white.dat"))


################################################

black.dat = fread('TablesA11A12black_dat.csv')
non.south.black.dat = black.dat[black.dat$south == 0,]

reg.black = lm(voted2012~diss*nonblack.to.black+diss*I(nonblack.to.black^2)+gender+familyincome+partyaffiliation+state,
          data = black.dat,
          weights = count)
clustered.se = cl(black.dat, reg.black, black.dat$dma_ID)
reg.black$se = clustered.se

reg.black.non.south = lm(voted2012~diss*nonblack.to.black+diss*I(nonblack.to.black^2)+gender+familyincome+partyaffiliation+state,
               data = non.south.black.dat,
               weights = count)
clustered.se = cl(non.south.black.dat, reg.black.non.south, non.south.black.dat$dma_ID)
reg.black.non.south$se = clustered.se

rm(list = c("black.dat","non.south.black.dat"))


#################################################


hispanic.dat = fread('TablesA11A12hispanic_dat.csv')
non.south.hispanic.dat = hispanic.dat[hispanic.dat$south == 0,]


reg.hispanic = lm(voted2012~diss*nonhispanic.to.hispanic+diss*I(nonhispanic.to.hispanic^2)+gender+familyincome+partyaffiliation+state,
          data = hispanic.dat,
          weights = count)
clustered.se = cl(hispanic.dat, reg.hispanic, hispanic.dat$dma_ID)
reg.hispanic$se = clustered.se

reg.hispanic.non.south = lm(voted2012~diss*nonhispanic.to.hispanic+diss*I(nonhispanic.to.hispanic^2)+gender+familyincome+partyaffiliation+state,
                  data = non.south.hispanic.dat,
                  weights = count)
clustered.se = cl(non.south.hispanic.dat, reg.hispanic.non.south, non.south.hispanic.dat$dma_ID)
reg.hispanic.non.south$se = clustered.se



rm(list = c("hispanic.dat","non.south.hispanic.dat"))

outtable = apsrtable(reg.black,
                     reg.hispanic,
                     reg.white,
                     Sweave = T,
                     stars = 'default',
                     notes = '',
                     omitcoef = 'state'
        )
writeLines(
  outtable, 'TableA11.tex')






outtable = apsrtable(reg.black.non.south,
                     reg.hispanic.non.south,
                    reg.white.non.south,
                     Sweave = T,
                     stars = 'default',
                     notes = '',
                     omitcoef = 'state'
)
writeLines(
  outtable, 'TableA12.tex')